/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.web.servlet;

import com.google.gson.Gson;
import com.web.daoimpl.CourseDaoImpl;
import com.web.entity.Course;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 *
 * @author liqianru
 */
@WebServlet("/getallCourse")
public class Admin_getallCourse extends HttpServlet {

    CourseDaoImpl service=new CourseDaoImpl();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String action=req.getParameter("action1");
        if(action.equals("getall")){
        List<Course> list=new ArrayList<>();
        HttpSession session=req.getSession(false);
        //
        list=service.getallCourse();
        //
        session.setAttribute("allCourse", list);
        System.out.println(list.get(0).getCourseName()+"--");
        //
        req.getRequestDispatcher("/admin_Course.jsp").forward(req, resp);}
        if(action.equals("getBycourseName")){
            Gson json=new Gson();
            String coursename=req.getParameter("coursename");
            List<Course> courseidList=service.getallBycoursename(coursename);
            System.out.println(courseidList.get(0).getCourseId());
            resp.setContentType("application/x-json;charset=utf-8");
            resp.getWriter().print(json.toJson(courseidList));
        }
        
    }
    
}
